﻿using DBO_ShopMob;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WP_Customer_ShopMob.Helper;

namespace WP_Customer_ShopMob.Model
{
    /// <summary>
    /// Repository de debug avec des données en dur
    /// </summary>
    class DebugRepository : IRepository
    {
        /// <summary>
        /// Liste des clients
        /// </summary>
        private List<Client> clients = new List<Client>();

        /// <summary>
        /// Liste des articles
        /// </summary>
        private List<Article> listArticles = new List<Article>();

        /// <summary>
        /// Liste des magasins
        /// </summary>
        private List<Magasin> listMagasin = new List<Magasin>();

        /// <summary>
        /// Stocks d'articles avec la quantité pour un magasin
        /// </summary>
        private Dictionary<long, int> stocks = new Dictionary<long, int>();

        /// <summary>
        /// Liste des catégories
        /// </summary>
        private List<Categorie> listCategories = new List<Categorie>();

        /// <summary>
        /// Construteur standard : on instancie quelques clients pour le debug
        /// </summary>
        public DebugRepository() {
            this.AjouterClient(new Client("qp", "M.", "LEPETIT", "Quentin", "0000", "qp", "Rue Du pont", "45000", "Regis", "0100000000", "0600000000", new DateTime(1990, 4, 5)));
            this.AjouterClient(new Client("ft", "M.", "TAVILLA", "Fabrice", "0000", "ft", "Rue Du pont", "45000", "Regis", "0100000000", "0600000000", new DateTime(1990, 4, 5)));
            this.AjouterClient(new Client("sv", "M.", "VALENTIN", "Steeve", "0000", "sv", "Rue Du pont", "45000", "Regis", "0100000000", "0600000000", new DateTime(1990, 4, 5)));
            this.AjouterClient(new Client("kpj", "M.", "PIERRE-JUSTIN", "Karl", "0000", "kpj", "Rue Du pont", "45000", "Regis", "0100000000", "0600000000", new DateTime(1990, 4, 5)));
            this.AjouterClient(new Client("kg", "M.", "GABIN", "Kris", "0000", "kg", "Rue Du pont", "45000", "Regis", "0100000000", "0600000000", new DateTime(1990, 4, 5)));
            listCategories.Add(new DBO_ShopMob.Categorie() { details = "sport", id = 1, idMere = 0, nom = "Sport" });
            listCategories.Add(new DBO_ShopMob.Categorie() { details = "sport", id = 2, idMere = 1, nom = "Football" });
            listCategories.Add(new DBO_ShopMob.Categorie() { details = "alim", id = 3, idMere = 0, nom = "Alimentaire" });
            listArticles.Add(new DBO_ShopMob.Article() { details = "Les chaussures", id = 1, nom = "chaussures", prix = 20, categorie = 2 });
            listArticles.Add(new DBO_ShopMob.Article() { details = "Les chaussettes", id = 2, nom = "chaussettes", prix = 5, categorie = 2 });
            listArticles.Add(new DBO_ShopMob.Article() { details = "Les casques", id = 3, nom = "casques", prix = 16 });
            listArticles.Add(new DBO_ShopMob.Article() { details = "Le ballon", id = 4, nom = "Ballon", prix = 10, categorie = 2 });
            listArticles.Add(new DBO_ShopMob.Article() { details = "La bille", id = 5, nom = "bille", prix = 5 });
            listArticles.Add(new DBO_ShopMob.Article() { details = "Le pantalon", id = 6, nom = "pantalon", prix = 20 });
            listArticles.Add(new DBO_ShopMob.Article() { details = "sandwitch grec", id = 7, nom = "kebab", prix = 5.5, categorie = 3 });
            listArticles.Add(new DBO_ShopMob.Article() { details = "classique de mcdonald", id = 8, nom = "big mac", prix = 2, categorie = 3 });
            stocks.Add(1, 15);
            stocks.Add(2, 80);
            stocks.Add(3, 40);
            stocks.Add(4, 26);
            listMagasin.Add(new DBO_ShopMob.Magasin() { idMagasin = "1", ville = "Dammartin en goele", stock = stocks });
            listMagasin.Add(new DBO_ShopMob.Magasin() { idMagasin = "2", ville = "paris" , stock = stocks });
            stocks = new Dictionary<long, int>();
            stocks.Add(5, 25);
            stocks.Add(2, 80);
            stocks.Add(8, 80);
            stocks.Add(7, 26);
            stocks.Add(4, 26);
            listMagasin.Add(new DBO_ShopMob.Magasin() { idMagasin = "3", ville = "massy", stock = stocks });
            stocks = new Dictionary<long, int>();
            stocks.Add(6, 10);
            stocks.Add(5, 150);
            stocks.Add(4, 40);
            listMagasin.Add(new DBO_ShopMob.Magasin() { idMagasin = "4", ville = "le raincy", stock = stocks });
            stocks = new Dictionary<long, int>();
            stocks.Add(5, 25);
            stocks.Add(2, 80);
            stocks.Add(4, 26);
            stocks.Add(8, 13);
            listMagasin.Add(new DBO_ShopMob.Magasin() { idMagasin = "5", ville = "saint leu la forêt", stock = stocks });
            stocks = new Dictionary<long, int>();
            stocks.Add(5, 25);
            stocks.Add(1, 10);
            stocks.Add(3, 5);
            stocks.Add(7, 10);
            listMagasin.Add(new DBO_ShopMob.Magasin() { idMagasin = "6", ville = "marseille", stock = stocks });
            stocks = new Dictionary<long, int>();
            stocks.Add(6, 25);
            stocks.Add(2, 180);
            stocks.Add(1, 206);
            listMagasin.Add(new DBO_ShopMob.Magasin() { idMagasin = "7", ville = "cherbourg", stock = stocks });
        }

        /// <summary>
        /// Permet d'ajouter des clients
        /// </summary>
        /// <param name="client">client à rajouter</param>
        private void AjouterClient(Client client)
        {
            clients.Add(client);
        }

        /// <summary>
        /// Voir IRepository
        /// </summary>
        public void AjouterClient(Client client, ResultHandler methodToExec)
        {
            clients.Add(client);
            methodToExec("1", null);
        }

        /// <summary>
        // Voir IRepository
        /// </summary>
        public void Connexion(string login, string motdePasse, ResultHandler methodToExec)
        {
            //on parcours la liste des clients
            foreach (Client c in clients) {
                if (c.Email == login && motdePasse == c.MotDePasse)
                {
                    //si on parviens à identifier l'utilisateur on retourne 1
                    methodToExec("1", null);
                    return;
                }
            }
            //sinon 0
            methodToExec("0", null);
        }

        /// <summary>
        // Voir IRepository
        /// </summary>
        public void GetClientParID(string login, ResultHandler methodToExec)
        {
            //on parcours la liste des clients
            foreach (Client c in clients)
            {
                if (c.Email == login) {
                    //si on parviens à identifier l'utilisateur on retourne l'objet client sous forme JSON
                    methodToExec(JsonConvert.SerializeObject(c), null);
                    return;
                }
            }
            //sinon on retourne rien
            methodToExec("", null);
        }


        /// <summary>
        // Voir IRepository
        /// </summary>
        public void GetCategoriesMere(ResultHandler methodToExec)
        {
            methodToExec(JsonConvert.SerializeObject(listCategories.Where(x => x.idMere == 0).ToList()), null);
        }

        /// <summary>
        // Voir IRepository
        /// </summary>
        public void GetCategoriesFille(long id, ResultHandler methodToExec)
        {
            methodToExec(JsonConvert.SerializeObject(listCategories.Where(x => x.idMere == id).ToList()), null);
        }

        /// <summary>
        // Voir IRepository
        /// </summary>
        public void GetArticlesByCateg(long id, ResultHandler methodToExec)
        {
            methodToExec(JsonConvert.SerializeObject(listArticles.Where(x => x.categorie == id).ToList()), null);
        }

        /// <summary>
        // Voir IRepository
        /// </summary>
        public void GetMagasinForArticle(long id, ResultHandler methodToExec)
        {
            methodToExec(JsonConvert.SerializeObject(listMagasin.Where(x => x.stock.Keys.Contains<long>(id))), null);
        }

        /// <summary>
        // Voir IRepository
        /// </summary>
        public void GetArticlesByNom(string nom, ResultHandler methodToExec)
        {
            methodToExec(JsonConvert.SerializeObject(listArticles.Where(x => x.nom.Contains(nom)).ToList()), null);
        }

        /// <summary>
        // Voir IRepository
        /// </summary>
        public void GetArticlesById(long idArticle, ResultHandler methodToExec)
        {
            methodToExec(JsonConvert.SerializeObject(listArticles.Where(x => x.id == idArticle).ToList()), null);
        }
    }
}
